<?php

namespace Common\Validator;


use Hyperf\Context\ApplicationContext;
use Hyperf\Validation\Contract\Rule;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;

class PasswordValidator implements Rule
{
    /**
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    public function passes($attribute, $value): bool
    {
        // 可以在此定义复杂度验证规则，这里假设密码长度至少为6
        $isComplexPassword = strlen($value) >= 6;

        if ($isComplexPassword) {
            // 进行哈希加密验证
            $hash = ApplicationContext::getContainer()->get('hash');
            $hashedValue = '待验证的哈希加密密码';

            return $hash->check($value, $hashedValue);
        }

        return false;
    }

    public function message(): array|string
    {
        // 定义验证失败时的错误信息
        return 'The :attribute must contain at least 6 characters and be a hashed value.';
    }
}
